package com.example.hm_dtfkz.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import com.android.base.util.ActivityClickUtil;
import com.android.base.util.DisplayUtil;
import com.example.hm_dtfkz.R;
import com.example.hm_dtfkz.config.AppConfig;
import com.example.hm_dtfkz.databinding.DialogShareBinding;
import com.example.hm_dtfkz.util.BitmapUtil;
import com.example.hm_dtfkz.util.QRCodeUtil;
import com.example.hm_dtfkz.util.ShareUtil;

/**
 * FileName: DialogShare
 * Author: admin
 * Date: 2022/1/23 14:33
 * Description:
 */
public class DialogShare extends Dialog implements View.OnClickListener {

    private DialogShareBinding shareBinding;
    private OnClickLister clickLister;
    private final Activity activity;
    private Bitmap mergeBitmap;
    private Bitmap qrCodeBitmap;
    private String inviteCode;
    private int shareType = 0;

    public DialogShare(Activity act) {
        super(act, R.style.dialog_loading);
        this.activity = act;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        shareBinding = DialogShareBinding.inflate(activity.getLayoutInflater());
        setContentView(shareBinding.getRoot());
        getWindow().getDecorView().setPadding(0, 0, 0, 0);

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);

        setCancelable(false);
        setCanceledOnTouchOutside(false);

        initView();
        initData();
        initListener();
    }

    private void initView() {
//        updateUI();
    }

    private void initData() {
        String baseUrl = "http://dw.winnershang.com/1064/" + "activity.getString(R.string.APP_INDEX)" + "/index.html";
//        if (HXCurrencySDK.isDebugMode()) {
//            baseUrl = "http://192.168.1.101/currency/invite/index.html";
//        }
        String content = baseUrl + "?code=" + inviteCode + "&channel=" + AppConfig.APP_CODE;
        Bitmap backBitmap = BitmapFactory.decodeResource(activity.getResources(), R.drawable.ic_share_bg);
        qrCodeBitmap = QRCodeUtil.createQRCodeBitmap(content, 200);
        mergeBitmap = BitmapUtil.mergeBitmap(activity, backBitmap, qrCodeBitmap);
        shareBinding.ivBg1.setImageBitmap(mergeBitmap);
//        shareBinding.ivQrcode.setImageBitmap(qrCodeBitmap);
//        shareBinding.tvLink.setText(content);
    }

    private void initListener() {
        shareBinding.ivShareQq.setOnClickListener(this);
        shareBinding.ivShareWechat.setOnClickListener(this);
//        shareBinding.ivShareQrcode.setOnClickListener(this);
//        shareBinding.ivShareLink.setOnClickListener(this);

//        shareBinding.ivDoShare.setOnClickListener(this);
//        shareBinding.ivCancel.setOnClickListener(this);
        shareBinding.ivClose.setOnClickListener(this);
    }

    public DialogShare setInviteCode(String inviteCode) {
        this.inviteCode = inviteCode;
        return this;
    }

    public DialogShare setClickLister(OnClickLister lister) {
        this.clickLister = lister;
        return this;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_do_share) {
            if (ActivityClickUtil.getInstance().isFastClick()) {
                //防止用户快速点击
                return;
            }
            // 实际分享、保存、复制操作
            switch (shareType) {
                case 0:
                    ShareUtil.shareImageToQQ(activity, mergeBitmap);
                    break;
                case 1:
                    ShareUtil.shareImageToWechat(activity, mergeBitmap);
                    break;
//                case 2:
//                    SoulPermission.getInstance().checkAndRequestPermissions(Permissions.build(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE), new CheckRequestPermissionsListener() {
//                        @Override
//                        public void onAllPermissionOk(Permission[] allPermissions) {
//                            FileUtil.saveBitmapToAlbum(activity, qrCodeBitmap);
//                        }
//
//                        @Override
//                        public void onPermissionDenied(Permission[] refusedPermissions) {
//                            ToastUtil.showShortToast(activity, "请先同意存储读写权限");
//                        }
//                    });
//                    break;
//                case 3:
//                    SystemUtil.copyToClipboard(activity, shareBinding.tvLink.getText().toString());
//                    ToastUtil.showShortToastCenter(activity, "邀请链接已复制到剪切板");
//                    break;
            }
        } else if (v.getId() == R.id.iv_close) {
            dismiss();
//        } else if (v.getId() == R.id.iv_share_qrcode) {
//            // 保存二维码图片
//            shareType = 2;
//            updateUI();
//        } else if (v.getId() == R.id.iv_share_link) {
//            // 复制分享链接
//            shareType = 3;
//            updateUI();
        } else if (v.getId() == R.id.iv_share_qq) {
            // qq分享
            shareType = 0;
//            updateUI();
            ShareUtil.shareImageToQQ(activity, mergeBitmap);
        } else if (v.getId() == R.id.iv_share_wechat) {
            // wechat分享
            shareType = 1;
//            updateUI();
            ShareUtil.shareImageToWechat(activity, mergeBitmap);
        }
    }

    private void updateUI() {
        switch (shareType) {
            case 0:
            case 1:
                shareBinding.ivBg1.setVisibility(View.VISIBLE);
                shareBinding.group2.setVisibility(View.GONE);
                shareBinding.group3.setVisibility(View.GONE);
                shareBinding.ivDoShare.setImageResource(R.drawable.icon_share_friend);
                break;
            case 2:
                shareBinding.ivBg1.setVisibility(View.GONE);
                shareBinding.group2.setVisibility(View.VISIBLE);
                shareBinding.group3.setVisibility(View.GONE);
                shareBinding.lcSlogan.setVisibility(View.VISIBLE);
                shareBinding.ivDoShare.setImageResource(R.drawable.icon_save_qrcode);
                break;
            case 3:
                shareBinding.ivBg1.setVisibility(View.GONE);
                shareBinding.group2.setVisibility(View.GONE);
                shareBinding.group3.setVisibility(View.VISIBLE);
                shareBinding.lcSlogan.setVisibility(View.VISIBLE);
                shareBinding.ivDoShare.setImageResource(R.drawable.icon_copy_link);
                break;
        }
    }

    public interface OnClickLister {
    }
}